home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.kei.com!wang!news
- From: emild@cs.technion.ac.il (Kohn Emil Dan)
- Subject: Re: help about structure (correction)
- Organization: Technion, Israel Institute of Technology
- Date: Sun, 31 Dec 1995 15:29:19 GMT
- Message-ID: <Pine.SV4.3.91-heb-2.04.951231172450.28898A-100000@cs.technion.ac.il>
- References: <4c3t9q$aob@chleuasme.francenet.fr>
- Sender: news@wang.com
-
-
-
-
- Sorry, my last advice had a little flaw (I hope this is less bugful :-)
-
-
- On 30 Dec 1995, jo wrote:
-
- > How can I translate from Pascal to C such a structure:
- > token=record;
- > begin
- > nature:tokennature;
- > case nature : tokennature of
- > tkinteger: (i:integer);
- > tkreal: (r:double);
- > ...
- > end;
- >
- > Can i use "switch" in a structure implementation ?
-
- No, you can't.
-
-
- A suggestion would be something like:
-
-
- typedef enum {tkinteger,tkreal,...} tokennature;
-
- struct token
- {
- tokennature nature;
-
- union
- {
- int i;
- double r;
- .
- .
- .
- }value;
- };
-
-
- And when you _USE_ the structure:
-
-
- .
- .
- .
- .
-
- struct token token;
- .
- .
- .
-
- switch (token.nature)
- {
-
- case tkinteger:
-
- token.value.i=4;
- .
- .
- .
- break;
-
- case tkreal:
-
- token.value.d=2.5;
- .
- .
- .
- break;
-
-
- .
- .
- .
- }
- .
- .
- .
-
- Best regards, and a HAPPY NEW YEAR!!!
-
-
- Emil
-